package org.frv.wispr.business;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository @Transactional
public class AccountEntryRepositoryHibernateImpl implements
		AccountEntryRepository {
	
	SessionFactory sessionFactory;
	

	public List<AccountEntry> findAll(Long accountId) {
        // for the moment, ignore the account id, there is no account
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AccountEntry.class);
        criteria.addOrder(Order.asc("index"));
        return (List<AccountEntry>) criteria.list();
    }
        
        public void save(AccountEntry accountEntry) {
            sessionFactory.getCurrentSession().saveOrUpdate(accountEntry);
       }

    public AccountEntry findById(Long id) {
        return (AccountEntry) sessionFactory.getCurrentSession().load(AccountEntry.class, id);
    }

    public List<AccountEntry> findAllByAccountId(Long accountId) {
        // for the moment, ignore the account id, there is no account
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AccountEntry.class);
        criteria.addOrder(Order.asc("index"));
        return (List<AccountEntry>) criteria.list();
    }

    public SessionFactory getSessionFactory() {
           return sessionFactory;
	   }

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

    

}
